package org.ltc.alliance.controller;

import org.ltc.alliance.constant.WebConst;
import org.ltc.alliance.model.PowerDomain;
import org.ltc.alliance.service.power.PowerService;
import org.ltc.alliance.utils.APIResponse;
import org.ltc.alliance.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/power")
public class PowerController extends BaseController{

    @Autowired
    PowerService powerService;

    @RequestMapping("/grant-releaser")
    @ResponseBody
    public APIResponse<?> transferReleaser(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String receiver = map.get("receiver");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try{
            powerService.transferReleaser(Integer.parseInt(uid), Integer.parseInt(receiver), Integer.parseInt(clubId));
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }

    @RequestMapping("/grant-manager")
    @ResponseBody
    public APIResponse<?> grantManager(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String receiver = map.get("receiver");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try{
            powerService.grantManager(Integer.parseInt(uid), Integer.parseInt(receiver), Integer.parseInt(clubId));
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }

    @RequestMapping("/revoke-manager")
    @ResponseBody
    public APIResponse<?> revokeManager(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String receiver = map.get("receiver");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try{
            powerService.revokeManager(Integer.parseInt(uid), Integer.parseInt(receiver), Integer.parseInt(clubId));
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }

    @RequestMapping("/user-power-list")
    @ResponseBody
    public APIResponse<?> getUserPowerList(
            HttpServletRequest request,
            HttpServletResponse response) {

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try {
            List<PowerDomain> powers = powerService.getPowerDomainListByUid(Integer.parseInt(uid));
            return APIResponse.success(powers);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

    @RequestMapping("/club-power-list")
    @ResponseBody
    public APIResponse<?> getClubPowerList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        try {
            List<PowerDomain> powers = powerService.getPowerDomainListByCid(Integer.parseInt(clubId));
            return APIResponse.success(powers);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

}
